package com.web.service.Impl;

import com.web.dao.LoginDao;
import com.web.dao.UserPermissionDao;
import com.web.pojo.domain.Permission;
import com.web.pojo.domain.UserInfo;
import com.web.pojo.vo.Menu;
import com.web.service.LoginServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wws
 * describe:
 */
@Service
public class LoginServerImpl implements LoginServer {
    @Autowired
    LoginDao loginDao;

    @Autowired
    UserPermissionDao userPermissionDao;

    /**
     * 查看用户菜单
     * @param userId
     * @return
     */
    @Override
    public List<Menu> getMenu(Integer userId) {
        List<Menu> list = new ArrayList<>();
        List<Permission> left = loginDao.getMenu(userId);
        for (Permission l : left) {
            Menu menu = new Menu();
            menu.setId(l.getId());
            menu.setPath(l.getUrl());
            menu.setAuthName(l.getName());
            List<Permission> button = loginDao.getButton(userId, l.getId());
            List<String> right = new ArrayList<>();
            for (Permission permission : button) {
                right.add(permission.getUrl());
            }
            menu.setRight(right);
            list.add(menu);
        }
        return list;
    }

    /**
     * 查看用户信息
     * @return 结果
     * @param userId
     */
    @Override
    public UserInfo getUserInfo(Integer userId) {
        return loginDao.getUserInfo(userId);
    }

    /**
     * 校验旧密码是否正确
     * @param userId 用户id
     * @return 结果
     */
    @Override
    public UserInfo checkPassword(Integer userId) {
        return loginDao.checkPassword(userId);
    }

    /**
     * 修改密码
     * @param userId 用户id
     * @param password 密码
     * @return 结果
     */
    @Override
    public Integer updatePassWord(Integer userId, String password) {
        return loginDao.updatePassWord(userId, password);
    }

    /**
     * 修改个人信息
     * @param userInfo 个人信息
     * @return 结果
     */
    @Override
    public Integer updatePersonalInfo(UserInfo userInfo) {
        return loginDao.updatePersonalInfo(userInfo);
    }
}
